using GalaSoft.MvvmLight;
using System.Collections.Generic;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Collections.ObjectModel;
using System.Windows;

namespace ContextMenuMvvm.ViewModel
{
	public class MainViewModel : ViewModelBase
	{
		private IList<City> cityList = null;
		private ICommand removeCommand = null;
		private ICommand showCommand = null;

		/// <summary>
		/// Initializes a new instance of the MainViewModel class.
		/// </summary>
		public MainViewModel()
		{
			////if (IsInDesignMode)
			////{
			////    // Code runs in Blend --> create design time data.
			////}
			////else
			////{
			////    // Code runs "for real"
			////}

			this.removeCommand = new RelayCommand<City>(this.RemoveAction);
			this.showCommand = new RelayCommand<City>(this.ShowAction);
		}

		public IEnumerable<City> CityList
		{
			get
			{
				if (this.cityList == null)
				{
					this.cityList = this.GetCityList();
				}
				return this.cityList;
			}
		}

		public ICommand RemoveCommand
		{
			get
			{
				return this.removeCommand;
			}
		}

		public ICommand ShowCommand
		{
			get
			{
				return this.showCommand;
			}
		}

		// NOTE: SelectedItem will not work
		// so we pass the city as command parameter
		private void RemoveAction(City city)
		{
			if (city != null)
			{
				this.cityList.Remove(city);
			}
		}

		private void ShowAction(City city)
		{
			if (city != null)
			{
				MessageBox.Show(string.Format("City: {0}", city.Name));
			}
		}

		private IList<City> GetCityList()
		{
			IList<City> cityList = new ObservableCollection<City>();
			cityList.Add(new City() { Name = "Madrid", Country = "ES", Language = "Spanish" });
			cityList.Add(new City() { Name = "Barcelona", Country = "ES", Language = "Spanish" });
			cityList.Add(new City() { Name = "Mallorca", Country = "ES", Language = "Spanish" });
			cityList.Add(new City() { Name = "Las Vegas", Country = "US", Language = "English" });
			cityList.Add(new City() { Name = "Dalas", Country = "US", Language = "English" });
			cityList.Add(new City() { Name = "New York", Country = "US", Language = "English" });
			cityList.Add(new City() { Name = "London", Country = "UK", Language = "English" });
			cityList.Add(new City() { Name = "Mexico", Country = "MX", Language = "Spanish" });
			cityList.Add(new City() { Name = "Milan", Country = "IT", Language = "Italian" });
			cityList.Add(new City() { Name = "Roma", Country = "IT", Language = "Italian" });
			cityList.Add(new City() { Name = "Paris", Country = "FR", Language = "French" });

			return cityList;
		}
	}

	public class City
	{
		public string Name
		{
			get;
			set;
		}

		public string Country
		{
			get;
			set;
		}

		public string Language
		{
			get;
			set;
		}
	}
}